<?php

namespace DatMoment\VideoBundle\Metier\Operations;

use DatMoment\VideoBundle\Entity\Categorie;
use Symfony\Component\Security\Core\Exception;


class CategorieOperations {
    
    private $categorie;
    private $entityManager;
    private $categorieRepository;
    
    public function __construct($pentityManager) {
        $this->entityManager = $pentityManager;
        $this->categorieRepository = $this->entityManager->getRepository('DatMomentVideoBundle:Categorie');
    }
    
    public function createCategorie($pname) {
        $this->categorie = new Categorie();
        $this->categorie->setName($pname);
    }
    
    public function getCategorie() {
        return $this->categorie;
    }
    
    public function initialisationCategorie($pcategorie) {
        $this->categorie = $pcategorie;
    }
    
    public function findAllCategorie() {
        return $this->categorieRepository->findAll();
    }
    
    public function persist() {
        if ($this->categorie != null) {
            $this->entityManager->persist($this->categorie);
            $this->entityManager->flush();
        }
        else {
            throw new Exception\InvalidArgumentException("CategorieOperations.php : L'objet est mal renseigné et n'a pas pu être persisté");
        }
    }
    
    public function findOneByName($pname) {
        $listCategories = $this->categorieRepository->findAll();
        foreach ($listCategories as $categorie) {
            if ($categorie->getName() == $pname) {
                return $categorie;
            }
        }  
    }
}

?>
